﻿using System;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.Threading;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using SysComm;
using SysOperationLayer.DBHandle;
using Resources;

public partial class SysLogs : Page, IRequiresSessionState
{
    protected override void InitializeCulture()
    {
        string strCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToString();

        try
        {
            if (Session["Culture"] != null)
            {
                strCulture = Session["Culture"].ToString();
            }
        }
        catch (NullReferenceException)
        {
        }

        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(strCulture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(strCulture);
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!SysUtil.IsAdministratorOrOperatorUserType())
        {
            base.Response.Redirect("ErrorPage/NotAllowed.aspx");
        }
        else
        {
            if (!IsPostBack)
            {
                ((Label)Master.FindControl("lblMstTitle")).Text = "| " + ISitemap.SysMaintenance.ToString() + "| " + ISitemap.SystemLog.ToString();

                LogDB objDB = new LogDB();

                ddlFunctionId.DataSource = objDB.GetFunctionInfo();
                ddlFunctionId.DataTextField = "FunctionName";
                ddlFunctionId.DataValueField = "FunctionID";
                ddlFunctionId.DataBind();

                objDB.CloseDB();

                txtDateFrom.Text = DateTime.Now.ToString("yyyy-MM-dd");
                txtDateTo.Text = txtDateFrom.Text;
                GetLogList("", txtDateFrom.Text, txtDateTo.Text);
            }
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        GetLogList(ddlFunctionId.SelectedValue, txtDateFrom.Text.Trim(), txtDateTo.Text.Trim());
    }

    private void GetLogList(string strFunid, string strDateFrom, string strDateTo)
    {
        if (strDateFrom != "")
        {
            strDateFrom += " 00:00:00";
        }
        if (strDateTo != "")
        {
            strDateTo += " 23:59:59";
        }

        LogDB objDB = new LogDB();
        gvResults.DataSource = objDB.getLogList(strFunid, strDateFrom, strDateTo);
        gvResults.DataBind();
        objDB.CloseDB();
    }

    protected void gvResults_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvResults.PageIndex = e.NewPageIndex;
        GetLogList(ddlFunctionId.SelectedValue, txtDateFrom.Text.Trim(), txtDateTo.Text.Trim());
    }
}
